package com.erhuogo.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.erhuogo.dao.ProvinceDao;
import com.erhuogo.entity.Province;
import com.erhuogo.util.DateUtils;

/**
 * 省份Dao的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:47:21
 */
public class ProvinceDaoImpl extends HibernateDaoSupport implements ProvinceDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(ProvinceDaoImpl.class);
	
	/**
	 * 1. 获取所有的省份信息Dao实现类
	 */
	@Override
	public List<Province> findAllProvince() {
		String hql = "from Province where status = ?";
		List<Province> result = null;
		List<Province> list = this.getHibernateTemplate().find(hql,0);
		if(list != null && list.size() > 0){
			logger.info("所有省份信息集合：" + list);
			result = list;
		}
		return result;
	}

	/**
	 * 2.根据省份id获取省份信息Dao实现类
	 */
	@Override
	public Province findProvinceById(Integer provinceId) {
		String hql = "from Province where id = ?";
		Province result = null;
		List<Province> list = this.getHibernateTemplate().find(hql, provinceId);
		if(list != null && list.size() > 0){
			logger.info("根据省份id:"+provinceId+",获取到省份信息：" + list.get(0));
			result = list.get(0);
		}
		return result;
	}

	/**
	 * 3.条件查询符合条件的省份记录条数Dao实现类
	 */
	@Override
	public int countByParams(String id, String name, String startDate, String endDate) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(Province.class);
		if(!StringUtils.isEmpty(id)){
			Integer provinceId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(provinceId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<Province> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的省份信息");
		} else{
			logger.info("没有找到符合查询条件求的省份信息");
		}
		return count;
	}

	/**
	 * 4.分页查询符合条件的省份记录Dao实现类
	 */
	@Override
	public List<Province> findPageByParams(String id, String name, String startDate, String endDate, int startIndex,
			int records) {
		List<Province> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(Province.class);
		if(!StringUtils.isEmpty(id)){
			Integer provinceId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(provinceId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<Province> list = this.getHibernateTemplate().findByCriteria(criteria, startIndex, records);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ list.size() +")个符合查询条件的省份信息");
		} else{
			logger.info("没有找到符合查询条件求的省份信息");
		}
		return result;
	}

	/**
	 * 5. 更新或者保存省份信息的Dao实现类
	 */
	@Override
	public void saveProvince(Province province) throws Exception {
		this.getHibernateTemplate().saveOrUpdate(province);
	}

	/**
	 * 6.根据id和状态修改省份信息的启用状态Dao实现类
	 */
	@Override
	public void updateStauts(Integer id, Integer statusCode) throws Exception {
		String hql = "from Province where id = ?";
		List<Province> list = this.getHibernateTemplate().find(hql, id);
		if(list!=null && list.size()>0){
			Province province = list.get(0);
			province.setStatus(statusCode);
			province.setAlterTime(DateUtils.DateToStr(new Date()));
			this.getHibernateTemplate().saveOrUpdate(province);
		}
	}

}
